<?php

namespace App\Http\Controllers\Api;


use App\Model\ContentCheck;
use App\Model\ImageExamine;
use App\Model\Number;
use App\Model\Room;
use App\Model\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class AuditController extends BaseController
{
    protected $imgModel;
    protected $number;

    public function __construct(Request $request)
    {
        parent::__construct($request);

        $this->imgModel = new  ImageExamine();
        $this->number = new  Number();
    }

    public function cover(Request $request)
    {
        $res = $this->imgModel->getRoomImgCoverList($request->all());
        foreach ($res['data'] as &$v) {
            $v->r_img = $this->imgPath($v->r_img);
            $v->i_img = $this->imgPath($v->i_img);
            $v->image = $this->imgPath($v->image);
            $v->create_date = date('Y-m-d H:i:s', $v->created_at);
        }
        return $this->toJson(self::SUCCESS, $res);
    }

    public function auditImgStatus(Request $request)
    {
        $res = $this->imgModel->audioStatus($request->all());
        return $this->toJson(self::SUCCESS, [], $res['msg']);
    }


    public function imgPath($img = '')
    {
        $imgPath = '';
        do {
            if (empty($img)) {
                $imgPath = '';
                break;
            };
            if (strpos($img, 'http:') !== false) {
                $imgPath = $img;
                break;
            } else {
                $imgPath = config('kkaudio.oss_url') . $img;
                break;
            }
        } while (false);

        return $imgPath;
    }


    //高频房间列表
    public function getHighRoomList(Request $request, Room $room)
    {
        $page = $request->input('page') ?? 1;
        $limit = $request->input('limit') ?? 10;
        $res = $room->getHighRoomList();
        if ($res['code'] == 0) {
            $room_ids = $res['data'];
            $room_info = $room->getRoomByRoomids($room_ids, $page, $limit);
            return $this->toJson(self::SUCCESS, $room_info);
        }
        return $this->toJson(self::SUCCESS, []);
    }

    //高频房间添加
    public function addHighRoom(Request $request, Room $room)
    {
        $room_id = $request->input('room_id', 0);
        return $this->toJson(self::SUCCESS, $room->addHighRoom($room_id));
    }

    //高频房间删除
    public function delHighRoom(Request $request, Room $room)
    {
        $room_id = $request->input('room_id', 0);
        return $this->toJson(self::SUCCESS, $room->delHighRoom($room_id));
    }


    public function getAuditUserLog(Request $request, Number $number)
    {
        $list = $number->getAuditUserLog($request->input());
        $i = 0;
        $data = [];
        foreach ($list['data'] as $v) {
            $img = json_decode($v['img'], true);
            if (empty($img)) {
                continue;
            }
            foreach ($img as $vv) {
                $data[$i]['id'] = $v['id'];
                $data[$i]['uuid'] = $v['uuid'];
                $data[$i]['img'] = build_image($vv);
                $i++;
            }
        }
        $list['data'] = $data;
        return $this->toJson(self::SUCCESS, $list);
    }

    public function setAuditUserImgType(Request $request, Number $number)
    {
        $id = $request->input('id');
        if ($number->setAuditUserImgType([$id])) {
            return $this->toJson(self::SUCCESS, []);
        }
        return $this->toJson(self::ERROR);
    }


    public function setAllAuditUserImgType(Request $request, Number $number)
    {
        $list = $number->getAuditUserLog($request->input());
        $ids = array_column($list['data'], 'id');
        if ($number->setAuditUserImgType($ids, 1)) {
            return $this->toJson(self::SUCCESS, []);
        }
        return $this->toJson(self::ERROR);
    }

    public function getAuditUserTextLog(Request $request, Number $number)
    {
        $list = $number->getAuditUserTextLog($request->input());
        return $this->toJson(self::SUCCESS, $list);
    }

    public function setAuditUserTextType(Request $request, Number $number)
    {
        $id = $request->input('id');
        if ($number->setAuditUserImgType([$id], 2, 2)) {
            return $this->toJson(self::SUCCESS, []);
        }
        return $this->toJson(self::ERROR);
    }


    public function setAllAuditUserTextType(Request $request, Number $number)
    {
        $list = $number->getAuditUserTextLog($request->input());
        $ids = array_column($list['data'], 'id');
        if ($number->setAuditUserImgType($ids, 1, 2)) {
            return $this->toJson(self::SUCCESS, []);
        }
        return $this->toJson(self::ERROR);
    }

    public function getAuditUserVoiceLog(Request $request, Number $number)
    {
        $data = $request->input();
        $data['type'] = 'voice_type';
        $list = $number->getAuditUserVoiceLog($data);
        foreach ($list['data'] as &$v) {
            if (empty($v['voice'])) {
                continue;
            }
            $v['voice'] = build_image($v['voice']);
        }
        return $this->toJson(self::SUCCESS, $list);
    }

    public function setAuditUserVoiceType(Request $request, Number $number)
    {
        $id = $request->input('id');
        if ($number->setAuditUserImgType([$id], 2, 3)) {
            return $this->toJson(self::SUCCESS, []);
        }
        return $this->toJson(self::ERROR);
    }


    public function setAllAuditUserVoiceType(Request $request, Number $number)
    {
        $list = $number->getAuditUserVoiceLog($request->input());
        $ids = array_column($list['data'], 'id');
        if ($number->setAuditUserImgType($ids, 1, 3)) {
            return $this->toJson(self::SUCCESS, []);
        }
        return $this->toJson(self::ERROR);
    }


    //审核文字内容相关
    //初审
    public function checkFirstContent(Request $request, ContentCheck $contentCheck)
    {
        $res = $contentCheck->getFirstContentList($request->input());
        return $this->toJson(self::SUCCESS, $res);
    }

    //复审
    public function checkSecondContent(Request $request, ContentCheck $contentCheck)
    {
        $res = $contentCheck->getSecondContentList($request->input());
        return $this->toJson(self::SUCCESS, $res);
    }


    public function checkFirstImg(Request $request, ContentCheck $contentCheck)
    {
        $list = $contentCheck->getFirstContentList($request->input());
        $i = 0;
        $data = [];
        foreach ($list['data'] as $v) {
            $img = $v['content'];
//            if (empty($img)) {
//                continue;
//            }
            $data[$i]['id'] = $v['id'];
            $data[$i]['uuid'] = $v['uuid'];
            $data[$i]['img'] = $img;
            $i++;
        }
        $list['data'] = $data;
        return $this->toJson(self::SUCCESS, $list);
    }


    public function checkSecondImg(Request $request, ContentCheck $contentCheck)
    {
        $list = $contentCheck->getSecondContentList($request->input());
        $i = 0;
        $data = [];
        foreach ($list['data'] as $v) {
            $img = $v['content'];
//            if (empty($img)) {
//                continue;
//            }
            $data[$i]['id'] = $v['id'];
            $data[$i]['uuid'] = $v['uuid'];
            $data[$i]['img'] = $img;
            $data[$i]['first_opt_name'] = $v['first_opt_name'];
            $data[$i]['first_date'] = $v['first_date'];
            $data[$i]['second_opt_name'] = $v['second_opt_name'];
            $data[$i]['second_date'] = $v['second_date'];
            $data[$i]['check_status'] = $v['check_status'];
            $data[$i]['review_check_status'] = $v['review_check_status'];
            $data[$i]['created_at'] = $v['created_at'];
            $data[$i]['f_created_at'] = $v['f_created_at'];
            $data[$i]['diff_time'] = $v['diff_time'];
            $i++;
        }
        $list['data'] = $data;
        return $this->toJson(self::SUCCESS, $list);
    }

    public function setAuditStatus(Request $request, ContentCheck $contentCheck)
    {
        $content_id = $request->input('content_id');
        $update_type = $request->input('u_type') ?? 1; //1单条  2全部
        $audit_type = $request->input('a_type') ?? 1; //1初审 2复审
        $status = $request->input('u_status') ?? 2; //1通过 2驳回
        $admin_user = $this->user;
        if ($update_type == 1) {
            //单条
            $res = $contentCheck->setContentStatus($content_id , $status, $audit_type,  $admin_user['id']);
        } else {
            if ($audit_type == 1) {
                $list = $contentCheck->getFirstContentList($request->input());
            } else {
                $list = $contentCheck->getSecondContentList($request->input());
            }
            $data = $list['data'];
            $ids = implode(',',array_column($data,'id'));
            $res = $contentCheck->setContentStatus($ids , $status, $audit_type,  $admin_user['id']);
        }
        if($res['code'] == 0){
            return $this->toJson(self::SUCCESS);
        }
        return $this->toJson(self::ERROR,$res['msg']);
    }

    public function adminUsers(Request $request, ContentCheck $contentCheck)
    {
        return $this->toJson(self::SUCCESS, $contentCheck->getAdminUser());

    }

    public function music(Request $request,ContentCheck $contentCheck){
        return $this->toJson(self::SUCCESS, $contentCheck->getMusic($request->all()));
    }

    public function setMusicStatus(Request $request,ContentCheck $contentCheck){
        $params = $request->all();
        if(empty($params) || empty($params['music_id']) || empty($params['status']) || empty($params['user_id'])){
            return $this->toJson(self::PARAM_ERROR, [], '缺少参数');
        }
        $admin = $this->user;
        $params['check_id'] = $admin['id'];
        $params['remark'] = empty($params['remark']) ? '' : $params['remark'];
        $res = $contentCheck->setMusicStatus($params);
        return $this->toJson(self::SUCCESS, [], $res);
    }

    public function delMusic($id){
        curl_request(getKkaudioApiUrl() . '/inner/music/delete', ['music_id'=>$id], 'json', 'POST');
        return $this->toJson(self::SUCCESS, []);
    }
}
